﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web.Mvc;
using System.Xml;
using SacNetUG.Models;
using System.Configuration;
using SacNetUG.Models.ViewModels.Home;

namespace SacNetUG.Controllers
{
    [HandleError]
    public partial class HomeController : Controller
    {
        public virtual ActionResult Index()
        {
            var viewModelEvents = (repository
                .GetRecentEvents()
                .Select(e => new IndexViewModel(e)).Distinct()).ToList().OrderByDescending(e => e.Date);

            if (TempData["RsvpMessage"] != null)
                ViewData["RsvpMessage"] = "<p>" + TempData["RsvpMessage"] + "</p>";

            return View(viewModelEvents);
        }

        public virtual ActionResult NotFound()
        {
            return View(repository.GetMissingKids());
        }

        public virtual ActionResult About()
        {
            return View();
        }

        //
        // GET: /Home/Contact

        [AcceptVerbs(HttpVerbs.Get)]
        public virtual ActionResult Contact()
        {
            return View();
        }

        //
        // POST: /Home/Contact
        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult Contact(Contact contact)
        {
            var contactReceiver = ConfigurationManager.AppSettings["AdminEmail"];
            var email = new MailMessage(contact.Email, contactReceiver)
                                    {
                                        Subject = "Contact request from sacnetug.org",
                                        Body = contact.ToString()
                                    };

            var emailHost = ConfigurationManager.AppSettings["EmailHost"];
            var emailPort = Convert.ToInt32(ConfigurationManager.AppSettings["EmailPort"]);
            var smtp = new SmtpClient(emailHost, emailPort);
            var emailUser = ConfigurationManager.AppSettings["EmailUser"];
            var emailPassword = ConfigurationManager.AppSettings["EmailPassword"];

            if(!string.IsNullOrEmpty(emailUser) && !string.IsNullOrEmpty(emailPassword))
                smtp.Credentials = new System.Net.NetworkCredential(emailUser, emailPassword);

            smtp.Send(email);

            ViewData["Message"] = "Thank you! A member from the Sac.Net organization will contact you shortly.";

            return View();
        }


        public virtual ActionResult MailingList()
        {
            return View(new User());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public virtual ActionResult MailingList(User user)
        {
            user.AuthID = "Mailing List";
            var db = new SacNetUGDataContext();
            if (db.Users.Count(p => p.EmailAddress.ToLower().Trim() == user.EmailAddress.ToLower().Trim()) == 0)
            {
                db.Users.InsertOnSubmit(user);
                db.SubmitChanges();
            }

            ViewData["Message"] = "Thank you! You will be notified of Sac.Net events.";

            return View();
        }

        public XmlDocument MissingKids { get; set; }
    }
}
